<?php

namespace app\admin\controller\auth;

use app\common\controller\BaseAdminController;
use app\common\model\AuthGroup;
use app\common\model\AuthGroupAccess;

class Admin extends BaseAdminController
{
    protected $editField = [
        'id',
        'nickname',
        'password',
        'avatar',
        'email',
        'status',
    ];
    protected $index_get_data = true;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\Admin();
        $allGroup = AuthGroup::where('status',1)
            ->select()->toArray();
        $allGroup = list_to_tree($allGroup,'id','pid','children');
        $this->assignconfig('allGroup',$allGroup);
    }
    public function index()
    {
        //visible
        if($this->request->isAjax()){
            $res = parent::index();
            foreach ($res['rows'] as $k=>$v){
                $v->group = $v->group->name;
                $v->visible([
                    'id',
                    'nickname',
                    'username',
                    'email',
                    'avatar',
                    'status',
                    'group',
                    'createtime',
                    'logintime'
                ]);
            }
            return $this->success('获取成功',$res);
        }
        return $this->view->fetch();
    }
    public function add()
    {
        if($this->request->isAjax()){
            $data = $this->request->param('row/a');
            if(isset($data['password'])){
                $salt = unique_random(5);
                $data['password'] = create_password($data['password'],$salt);
                $data['salt'] = $salt;
            }
            $has = $this->model->where('username',$data['username'])
                ->find();
            if(!empty($has)){
                return $this->error("用户名已存在");
            }
            $res = $this->model->create($data);
            if($res){
                if(isset($data['group_id'])){
                    AuthGroupAccess::create([
                        'uid'=>$res->id,
                        'group_id'=>$data['group_id']
                    ]);
                }
                return $this->success("添加成功");
            }else{
                return $this->error("添加失败");
            }
        }
        return $this->view->fetch();
    }
    public function edit($ids=null)
    {
        if(empty($ids)){
            $ids = $this->request->param('ids');
        }
        $row = $this->model->field($this->editField)->find($ids);
        if($this->request->isAjax()){
            $data = $this->request->param('row/a');
            if(!empty($data['password'])){
                $salt = unique_random(5);
                $data['password'] = create_password($data['password'],$salt);
                $data['salt'] = $salt;
            }else{
                if(isset($data['password'])){
                    unset($data['password']);
                }
            }
            $res = $row->save($data);
            if($res){
                if(isset($data['group_id'])){
                    AuthGroupAccess::where('uid',$ids)
                        ->update([
                            'group_id'=>$data['group_id']
                        ]);
                }
                return $this->success("修改成功");
            }else{
                return $this->error("修改失败");
            }
        }
        $this->assignconfig('row',$row);
        $group_id = AuthGroupAccess::where('uid',$ids)
            ->value('group_id');
        $this->assignconfig('group_id',$group_id);
        return $this->view->fetch();
    }
}